package com.fan.leetcode;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * 1838. 最高频元素的频数
 *
 * @author ZhangFan
 * @since 2021/7/19 下午 10:20
 */
public class Question1838 {

    public int maxFrequency(int[] nums, int k) {
        Arrays.sort(nums);
        int count = 0;
        Set<Integer> set = new HashSet<>();
        for (int i = nums.length - 1; i >= 0; i--) {
            if (set.contains(nums[i])) {
                continue;
            }
            set.add(nums[i]);
            int temp = 1, flag = 0;
            for (int j = i - 1; j >= 0; j--) {
                flag += nums[i] - nums[j];
                if (flag <= k) {
                    temp++;
                } else {
                    break;
                }
            }
            if (temp > count) {
                count = temp;
            }
        }
        return count;
    }
}
